<template>
  <div>
    <el-dialog
      title="查看视频"
      :visible.sync="visible"
      :show="show"
      width="70%"
      :before-close="handleClose"
      @close="$emit('update:show', false)"
    >

      <el-row :gutter="20">
        <el-col :span="18">
          <video-player
            ref="videoPlayer"
            class="video-player vjs-custom-skin"
            :playsinline="true"
            :options="playerOptions"
            @play="onPlayerPlay($event)"
            @pause="onPlayerPause($event)"
          />
          <div class="demo-input-suffix">
            hls 地址
            <el-input v-model="camera.hls" prefix-icon="el-icon-video-camera" :readonly="true" size="mini" />
          </div>
          <div class="demo-input-suffix">
            rtmp 地址
            <el-input v-model="camera.rtmp" prefix-icon="el-icon-video-camera" :readonly="true" size="mini" />
          </div>
        </el-col>
        <el-col :span="6">
          <el-collapse>
            <el-collapse-item title="控制">
              <el-row>
                <el-col :span="8"><el-button type="info" class="el-icon-top-left" size="mini" @click="control('top-left')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-top" size="mini" @click="control('top')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-top-right" size="mini" @click="control('top-right')" /></el-col>
              </el-row>
              <el-row>
                <el-col :span="8"><el-button type="info" class="el-icon-back" size="mini" @click="control('left')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-full-screen" size="mini" @click="control('no')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-right" size="mini" @click="control('right')" /></el-col>
              </el-row>
              <el-row>
                <el-col :span="8"><el-button type="info" class="el-icon-bottom-left" size="mini" @click="control('bottom-left')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-bottom" size="mini" @click="control('bottom')" /></el-col>
                <el-col :span="8"><el-button type="info" class="el-icon-bottom-right" size="mini" @click="control('bottom-right')" /></el-col>
              </el-row>
            </el-collapse-item>
            <el-collapse-item title="变焦">
              <el-row>
                <el-col :span="2" />
                <el-col :span="10" align="center">
                  <el-button type="info" class="el-icon-plus" size="mini" @click="control()" />
                </el-col>
                <el-col :span="10" align="center">
                  <el-button type="info" class="el-icon-minus" size="mini" @click="control()" />
                </el-col>
                <el-col :span="2" />
              </el-row>
            </el-collapse-item>
          </el-collapse>
        </el-col>
      </el-row>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { snap } from '@/api/device'
import 'video.js/dist/video-js.css'

export default {
  props: {
    show: Boolean,
    camera: Object
  },
  data() {
    return {
      visible: this.show,
      playerOptions: {
        // playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: true, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        // aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        html5: {
          hls: { withCredentials: false }
        },
        sources: [{
          type: 'application/x-mpegURL', // 这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看git网址项目
          src: '' // url地址
        }],
        hls: true,
        techOrder: ['html5'],
        poster: '../../assets/prepare.png', // 你的封面地址
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          fullscreenToggle: true // 全屏按钮
        }
      }
    }
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player
    }
  },
  watch: {
    show(item) {
      this.visible = this.show
      this.playerOptions.sources[0].src = this.camera.hls
    }
  },
  methods: {
    snap(deviceid, channel) {
      if (this.viewItem) {
        snap({ deviceid: this.viewItem.parentid, channel: this.viewItem.deviceid }).then((res) => {
          this.$message('截图操作成功')
        })
      }
    },
    control(direction) {
      this.$message('尚未实现')
    },
    handleClose(done) {
      done()
    },
    onPlayerPlay(player) {
      console.log('player play', player)
    },
    onPlayerPause(player) {
      console.log('player pause', player)
    },
    onPlayerEnded($event) {
      console.log($event)
    },
    // DOM元素上的readyState更改导致播放停止
    onPlayerWaiting($event) {
      console.log($event)
    },
    // 已开始播放回调
    onPlayerPlaying($event) {
      console.log($event)
    },
    // 当播放器在当前播放位置下载数据时触发
    onPlayerLoadeddata($event) {
      console.log($event)
    },
    // 当前播放位置发生变化时触发。
    onPlayerTimeupdate($event) {
      console.log($event)
    },
    // 媒体的readyState为HAVE_FUTURE_DATA或更高
    onPlayerCanplay(player) {
      // console.log('player Canplay!', player)
    },
    // 媒体的readyState为HAVE_ENOUGH_DATA或更高。这意味着可以在不缓冲的情况下播放整个媒体文件。
    onPlayerCanplaythrough(player) {
      // console.log('player Canplaythrough!', player)
    },
    // 播放状态改变回调
    playerStateChanged(playerCurrentState) {
      console.log('player current update state', playerCurrentState)
    },
    // 将侦听器绑定到组件的就绪状态。与事件监听器的不同之处在于，如果ready事件已经发生，它将立即触发该函数。。
    playerReadied(player) {
      console.log('example player 1 readied', player)
    }
  }
}
</script>
<style>
  .video-js .vjs-icon-placeholder {
      width: 100%;
      height: 100%;
      display: block;
  }
</style>
